<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/4/10
 * 描述:短信服务
 */

namespace app\service;

class SmsService
{
    use BaseService;

    public static function send_code($mobile, $code): array
    {
        if (empty($mobile)) {
            return self::error_return('手机号码不能为空');
        }
        //验证手机号码
        if (!preg_match("/^1[3456789]{1}\d{9}$/", $mobile)) {
            return self::error_return('手机号码格式不正确');
        }
        $template = config('sms.yunpian.template');
        if (empty($template)) {
            return self::error_return('短信模板未配置');
        }
        if (empty($code)) {
            return self::error_return('验证码不能为空');
        }
        $template = str_replace('#code#', $code, $template);

        $res = self::yunpian_single_send($mobile, $template);
        if ($res['code'] <> 0) {
            return $res;
        }
        return self::success_return('发送成功');
    }

    public static function yunpian_single_send($mobile, $text)
    {
        $apikey = config('sms.yunpian.apikey');
        $params = [
            'apikey' => $apikey,
            'mobile' => $mobile,
            'text' => $text,
        ];
        $res = bs_curl_post("https://sms.yunpian.com/v2/sms/single_send.json", http_build_query($params), 10, [
            'Accept:text/plain;charset=utf-8',
            'Content-Type:application/x-www-form-urlencoded',
            'charset=utf-8'
        ]);
        if (!$res) {
            return self::error_return('发送失败');
        }
        bs_log($res, 'yunpian_single_send', 'sms.log');
        $res = json_decode($res, true);
        return self::success_return('发送成功');
    }


}

